package com.woniuxy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Member;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.User;
import com.woniuxy.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author: GYF
 * Date: 2021/2/23:10:57
 * Description:用户操作控制层
 */

@RestController
@RequestMapping("user")
@Api(tags = "可登入用户操纵")
public class UserController {
    @Autowired
    UserServiceImpl userService;

    /**
     * Description : 登入接口
     * ChangeLog : 1. 创建 (2021/2/23 14:53 GYF);
     * @param user
     * @return java.lang.String
     **/
    @PostMapping("login")
    public String login(User user, @ApiIgnore HttpSession session){
        int i = userService.login(user,session);
        if(i==1){
            return "登入成功";
        }else{
            return "登入失败";
        }
    }
    
    /**
     * Description : 添加管理员账号
     * ChangeLog : 1. 创建 (2021/2/24 14:19 GYF);
     * @param user  
     * @return com.woniuxy.entity.ResponseEntity
     **/
    @PostMapping("reguser")
    public ResponseEntity registerUser(User user){
        int i = userService.registerUser(user);
        if(i>0){
            return new ResponseEntity();
        }else {
            ResponseEntity responseEntity = new ResponseEntity();
            responseEntity.setCode(500);
            return responseEntity;
        }
    }

    /**
     * Description : 分页查询所有后台用户
     * ChangeLog : 1. 创建 (2021/2/24 15:09 GYF);
     *
     * @return com.woniuxy.entity.ResponseEntity
     **/
    @PostMapping("queryall")
    public ResponseEntity queryallUser(int page){
        PageHelper.startPage(page,5);
        List<User> users = userService.queryAdmin();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return new ResponseEntity(pageInfo);
    }




}
